// 全局给String拓展,errorToast方法
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:convert';
import 'dart:io';

// initToast 初始化toast
FToast initToast(BuildContext context) {
  FToast fToast = FToast();
  fToast.init(context);
  return fToast;
}

Widget createToastWidget(String text, Color color, Icon icon) {
  return Container(
    padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(25.0),
      color: color,
    ),
    child: Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        icon,
        const SizedBox(
          width: 12.0,
        ),
        // Text(text), 白色字
        Text(
          text,
          style: const TextStyle(color: Colors.white),
        ),
      ],
    ),
  );
}

extension StringExtension on String {
  void errorToast(BuildContext context) {
    FToast fToast = initToast(context);
    fToast.showToast(
      child: createToastWidget(
          this,
          Colors.red,
          const Icon(
            Icons.error_outline,
            color: Colors.white,
          )),
      gravity: ToastGravity.BOTTOM,
      toastDuration: const Duration(seconds: 2),
    );
  }

  void successToast(BuildContext context) {
    FToast fToast = initToast(context);
    fToast.showToast(
      child: createToastWidget(
          this,
          Colors.green,
          const Icon(
            Icons.check_circle,
            color: Colors.white,
          )),
      gravity: ToastGravity.BOTTOM,
      toastDuration: const Duration(seconds: 2),
    );
  }

  void infoToast(BuildContext context) {
    FToast fToast = initToast(context);
    fToast.showToast(
      child: createToastWidget(
          this,
          Colors.blue,
          const Icon(
            Icons.info_outline_rounded,
            color: Colors.white,
          )),
      gravity: ToastGravity.BOTTOM,
      toastDuration: const Duration(seconds: 2),
    );
  }

  String base64Encode() {
    var content = utf8.encode(this);
    var digest = base64.encode(content);
    return digest;
  }

  String base64Decode() {
    var bytes = base64.decode(this);
    var result = utf8.decode(bytes);
    return result;
  }

  List<int> base64DecodeToBytes() {
    var bytes = base64.decode(this);
    return bytes;
  }
}

// 字节码转换String Base64
extension ByteExtension on List<int> {
  String base64Encode() {
    var digest = base64.encode(this);
    return digest;
  }
}
